package com.china08.yxyapi.api.home.room;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.meeting.VideoMeeting;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.VideoMeetingRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 修改视频会议状态
 *
 */
@RestController
public class VideoMeetingv1_1RoomUdpController {

    @Autowired
    private VideoMeetingRepository videoMeetingRepository;

    @RequestMapping(value = "/api_yw/home/room/videomeetv1_1/udp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {

        reqModel.check();

        VideoMeeting meeting = videoMeetingRepository.findByRoomId(reqModel.room_id);
        if (meeting == null) { return; }
        if (reqModel.op.equals("start")) {
            meeting.setStatus(1);
        } else if (reqModel.op.equals("end")) {
            Date nowDate = DateUtils.formatStrDate(reqModel.op_time);
            Date overDate = meeting.getEndDate();
            if (nowDate.getTime() >= overDate.getTime()) {
                meeting.setStatus(2);
            }
            meeting.setActualEndDate(nowDate);// 在下课后一小时执行检查
        }
        videoMeetingRepository.save(meeting);
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 会议id
        public String room_id;
        // 操作类型
        public String op;
        // 操作时间
        public String op_time;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(room_id, null), "会议编号不能为空");
        }
    }
}
